import path from "path";

import { createDefineMock } from "vite-plugin-mock-dev-server";

const defineMock = createDefineMock((mock) => {
  mock.url = path.join(import.meta.env.VITE_APP_BASE_API, mock.url);
});

const dicts = [
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:13",
    updateBy: null,
    updateTime: null,
    remark: "用户性别列表",
    dictId: 1,
    dictName: "用户性别",
    dictType: "sys_user_sex",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:14",
    updateBy: null,
    updateTime: null,
    remark: "菜单状态列表",
    dictId: 2,
    dictName: "菜单状态",
    dictType: "sys_show_hide",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:14",
    updateBy: null,
    updateTime: null,
    remark: "系统开关列表",
    dictId: 3,
    dictName: "系统开关",
    dictType: "sys_normal_disable",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:15",
    updateBy: null,
    updateTime: null,
    remark: "任务状态列表",
    dictId: 4,
    dictName: "任务状态",
    dictType: "sys_job_status",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:15",
    updateBy: null,
    updateTime: null,
    remark: "任务分组列表",
    dictId: 5,
    dictName: "任务分组",
    dictType: "sys_job_group",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:16",
    updateBy: null,
    updateTime: null,
    remark: "系统是否列表",
    dictId: 6,
    dictName: "系统是否",
    dictType: "sys_yes_no",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:17",
    updateBy: null,
    updateTime: null,
    remark: "通知类型列表",
    dictId: 7,
    dictName: "通知类型",
    dictType: "sys_notice_type",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:17",
    updateBy: null,
    updateTime: null,
    remark: "通知状态列表",
    dictId: 8,
    dictName: "通知状态",
    dictType: "sys_notice_status",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:18",
    updateBy: null,
    updateTime: null,
    remark: "操作类型列表",
    dictId: 9,
    dictName: "操作类型",
    dictType: "sys_oper_type",
    status: "0",
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:19",
    updateBy: null,
    updateTime: null,
    remark: "登录状态列表",
    dictId: 10,
    dictName: "系统状态",
    dictType: "sys_common_status",
    status: "0",
  },
];

const dictData = [
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:24",
    updateBy: null,
    updateTime: null,
    remark: "性别男",
    dictCode: 1,
    dictSort: 1,
    dictLabel: "男",
    dictValue: "0",
    dictType: "sys_user_sex",
    cssClass: "",
    listClass: "",
    isDefault: "Y",
    status: "0",
    default: true,
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:24",
    updateBy: null,
    updateTime: null,
    remark: "性别女",
    dictCode: 2,
    dictSort: 2,
    dictLabel: "女",
    dictValue: "1",
    dictType: "sys_user_sex",
    cssClass: "",
    listClass: "",
    isDefault: "N",
    status: "0",
    default: false,
  },
  {
    createBy: "admin",
    createTime: "2023-04-23 16:13:25",
    updateBy: null,
    updateTime: null,
    remark: "性别未知",
    dictCode: 3,
    dictSort: 3,
    dictLabel: "未知",
    dictValue: "2",
    dictType: "sys_user_sex",
    cssClass: "",
    listClass: "",
    isDefault: "N",
    status: "0",
    default: false,
  },
];
export default defineMock([
  {
    url: "/api/dict",
    method: ["GET"],
    body: ({ query }) => {
      const { dictType } = query;

      const dicts = dictType.split(",");

      const datas: Record<string | number, unknown> = {};

      dicts.forEach((dict: string) => {
        datas[dict] = dictData;
      });

      return { code: 200, msg: "success", data: datas };
    },
  },
  {
    url: "/api/v1/dict/:code/options",
    method: ["GET"],
    body: ({ params }) => {
      const typeCode = params.cde;

      let list = null;

      if (typeCode == "gender") {
        list = [
          {
            value: "1",
            label: "男",
          },
          {
            value: "2",
            label: "女",
          },
          {
            value: "0",
            label: "未知",
          },
        ];
      }

      return {
        code: "00000",
        data: list,
        msg: "一切ok",
      };
    },
  },
  {
    url: "/api/v1/dict/types/page",
    method: ["GET"],
    body: () => {
      return {
        code: "00000",
        data: {
          list: dicts,
          total: dicts.length,
        },
        msg: "success",
      };
    },
  },
  {
    url: "/api/v1/dict/page",
    method: ["GET"],
    body: () => {
      return {
        code: "00000",
        data: {
          list: dictData,
          total: dictData.length,
        },
        msg: "success",
      };
    },
  },
  {
    url: "/api/v1/dict/:code/form",
    method: ["GET"],
    body: ({ params }) => {
      const dictCode = params.code;

      const item = dictData.find((item) => item.dictCode == dictCode);
      return {
        code: "00000",
        data: item,
        msg: "一切ok",
      };
    },
  },
  {
    url: "/api/v1/dict/:ids",
    method: ["DELETE"],
    body: () => {
      return {
        code: "00000",
        msg: "删除成功",
      };
    },
  },
  {
    url: "/api/v1/dict",
    method: ["POST"],
    body: () => {
      return {
        code: "00000",
        msg: "添加成功",
      };
    },
  },
  {
    url: "/api/v1/dict/:code",
    method: ["PUT"],
    body: () => {
      return {
        code: "00000",
        msg: "更新成功",
      };
    },
  },
]);
